package com.nageoffer.shortLink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nageoffer.shortLink.admin.common.convention.result.Result;
import com.nageoffer.shortLink.admin.common.convention.result.Results;

import com.nageoffer.shortLink.admin.dto.req.UserLoginReqDTO;
import com.nageoffer.shortLink.admin.dto.req.UserRegisterReqDTO;
import com.nageoffer.shortLink.admin.dto.req.UserUpdataReqDTO;
import com.nageoffer.shortLink.admin.dto.resp.UserActualRespDTO;
import com.nageoffer.shortLink.admin.dto.resp.UserLoginRespDTO;
import com.nageoffer.shortLink.admin.dto.resp.UserRespDTO;
import com.nageoffer.shortLink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;


    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username){
        return Results.success(userService.getUserByUsername(username));
    }

    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username){
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username),UserActualRespDTO.class));
    }

    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> updata(@RequestBody UserUpdataReqDTO requestParam){
        userService.updata(requestParam);
        return Results.success();
    }

    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }

    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username") String username,@RequestParam("token") String token){
        return Results.success(userService.checkLogin(username,token));

    }


    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }




}
